package com.hfut.manage.controller;


import com.hfut.manage.pojo.Result;
import com.hfut.manage.pojo.Teacher;
import com.hfut.manage.pojo.TeacherDTO;
import com.hfut.manage.pojo.TeacherQueryParam;
import com.hfut.manage.service.IEducationService;
import com.hfut.manage.service.IFamilyService;
import com.hfut.manage.service.ITeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @GetMapping
    public Result list(TeacherQueryParam teacherQueryParam) {
        List<Teacher> teacherList = teacherService.lambdaQuery().like(Teacher::getName,teacherQueryParam.getName()).list();
        return Result.success(teacherList);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.success(teacher);
    }

    @GetMapping("/name/{name}")
    public Result getByName(@PathVariable("name") String name) {
        Teacher teacher = teacherService.lambdaQuery().eq(Teacher::getName, name).one();
        return Result.success(teacher);
    }

    @PostMapping
    public Result save(@RequestBody Teacher teacher) {
        boolean success = teacherService.saveTeahcherDTO(teacher);
        if (!success) {
            return Result.error(500, "新增教师数据失败");
        }
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result editTeacher(@PathVariable("id") Integer id, @RequestBody Teacher teacher) {
        boolean success = teacherService.updateById(teacher);
        if (!success) {
            return Result.error(500, "修改教师数据失败");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Integer id) {
        teacherService.removeTeacher(id);
        return Result.success();
    }

    @GetMapping("/DashData")
    public Result getDashDate(){
        try{
            List<Map<String, Object>> date = teacherService.getDashDate();
//            System.out.println("===================data:====================="+date);
            return Result.success(date);
        }
        catch (Exception e){
            return Result.error(500, e.getMessage());
        }
    }
}
